﻿using System.IO;
using System.IO.Compression;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Configuration;

namespace Channelvn.Cached.Common
{
    public class Utilities
    {
        public static Page LoadPage(string url, HttpContext context)
        {
            return (Page)PageParser.GetCompiledPageInstance(url, context.Server.MapPath(url), context);
        }

        public static string RenderPage(Page page, HttpContext context)
        {
            var writer = new StringWriter();
            context.Server.Execute(page, writer, true);
            var htmlCode = writer.ToString().Trim();
            writer.Close();
            if (ConfigurationManager.AppSettings["AllowWrite1Line"] == "1")
            {
                htmlCode = htmlCode.Replace("\t", string.Empty);
                htmlCode = htmlCode.Replace("    ", string.Empty);
                htmlCode = htmlCode.Replace("  ", " ");
            }
            return htmlCode;
        }

        public static byte[] Zip(string str)
        {
            byte[] buffer = Encoding.UTF8.GetBytes(str);
            using (var ms = new MemoryStream())
            {
                using (var zip = new GZipStream(ms, CompressionMode.Compress, true))
                {
                    zip.Write(buffer, 0, buffer.Length);
                }
                ms.Position = 0;
                var bytesContent = ms.ToArray();
                return bytesContent;
            }
        }

        public static string UnZip(byte[] bytes)
        {
            using (var msi = new MemoryStream(bytes))
            using (var mso = new MemoryStream())
            {
                using (var gs = new GZipStream(msi, CompressionMode.Decompress))
                {
                    gs.CopyTo(mso);
                }
                return Encoding.UTF8.GetString(mso.ToArray());
            }
        }
    }

    public class RewriteFormHtmlTextWriter : HtmlTextWriter
    {
        private readonly string _formAction;

        public RewriteFormHtmlTextWriter(TextWriter writer)
            : base(writer)
        {

        }
        public RewriteFormHtmlTextWriter(TextWriter writer, string action)
            : base(writer)
        {
            if (!string.IsNullOrEmpty(action))
            {
                this._formAction = action;
            }
        }

        public override void RenderBeginTag(string tagName)
        {
            if (tagName.IndexOf("form") >= 0)
            {
                base.RenderBeginTag(tagName);
            }
        }

        public override void WriteAttribute(string name, string value, bool fEncode)
        {
            if ("action" == name && !string.IsNullOrEmpty(_formAction))
            {
                value = "";
                //value = this._formAction;
            }
            base.WriteAttribute(name, value, fEncode);
        }
    }
}
